<?php foreach($im_cases as $ic):
$userrole = FALSE;
$case_stats = FALSE; ?>
<div class="row animate fadeIn">
    <div class="ibox-title" style="border:none;"><h5 style="color: #00afef"><i>View</i></h5>
        <div class="ibox-tools">   
             <!-- <button class="btn btn-primary btn-sm save-btn-tab" id="save_im_edit">Save</button>
             <a id="cancel_view" class="btn btn-sm btn-outline btn-info cancel-btn-tab">Cancel</a> -->
             <a id="complete_btn_v" class="btn btn-sm btn-outline btn-success cancel-btn-tab">Close</a>
        </div>
     </div>
     <div class="ibox-content">
      <form class="form-horizontal" id="im_request_form_view">
      <input type="hidden" name="case_id" value="<?php echo $ic->case_id; ?>">
      <div class="row">
        <div class="col-sm-6">
          <div class="form-group">
            <label class="col-lg-4 control-label">Case No.</label>
              <div class="col-lg-5">
                 <input readonly type="text" name="case_no" value="<?php echo $ic->case_no; ?>" class="form-control">
              </div>
          </div>
        </div>
        <div class="col-sm-6">
          <div class="form-group">
            <label class="col-lg-4 control-label">Reported Date</label>
              <div class="col-lg-8">
                <input type="date" value="<?php echo $ic->reported_date; ?>" name="reported_date" class="form-control">
              </div>
          </div>
        </div>
      </div>
      <div class="row">
        <div class="col-sm-6">
          <div class="form-group">
          <label class="col-lg-4 control-label">Case Type</label>
            <div class="col-lg-8">
              <select name="case_type" class="form-control chosen">
                <option value="">--</option>
                <?php foreach($case_type as $ct): ?>
                <option value="<?php echo $ct->lookup_code; ?>"  <?php echo ($ic->case_type == $ct->lookup_code ? 'selected' : ''); ?> ><?php echo $ct->meaning; ?></option>
                <?php endforeach; ?>
              </select>
            </div>
        </div>
        </div>
        <div class="col-sm-6">
          <!-- <input type="hidden" name="caller_acct_id" />
            <div class="form-group">
              <label class="col-lg-4 control-label">Caller Account ID</label>
                <div class="col-lg-8">
                  <div class="input-group">
                    <input type="text" name="caller_id" class="form-control" value="<?php echo $ic->caller_acct_id_m; ?>">
                    <span class="input-group-btn">
                      <button type="button" class="btn btn-success btn-sm" id="search_num"><i class="fa fa-search"></i></button>
                    </span>
                  </div>
                </div>
            </div>-->
            <div class="form-group">
            <label class="col-lg-4 control-label">Case Date</label>
              <div class="col-lg-8">
                <input type="date" value="<?php echo $ic->case_date; ?>" name="case_date" class="form-control">
              </div>
          </div>
          </div> 
      </div>
      <div class="row">
        <div class="col-sm-6">
          <div class="form-group">
            <label class="col-lg-4 control-label">Caller Name</label>
              <div class="col-lg-8">
                <input type="text" name="caller_name" value="<?php echo $ic->caller_name; ?> " class="form-control">
              </div>
          </div>
        </div>
        <div class="col-sm-6">
          <div class="form-group">
            <label class="col-lg-4 control-label">Caller Account ID</label>
              <div class="col-lg-8">
                <input type="text" name="caller_id" value="<?php echo $ic->caller_acct_id; ?> " class="form-control text-right">
              </div>
          </div>
        </div>
      </div>
      <div class="row">
        <div class="col-sm-6">
          <div class="form-group">
            <label class="col-lg-4 control-label">Caller Contact</label>
              <div class="col-lg-8">
                <input type="text" name="caller_contact" value="<?php echo $ic->caller_contact; ?>" class="form-control">
              </div>
          </div>
        </div>
        <div class="col-sm-6">
          <div class="form-group">
            <label class="col-lg-4 control-label">Caller Address</label>
              <div class="col-lg-8">
                <textarea name="caller_address" value="" class="form-control">
                </textarea>
              </div>
          </div>
        </div>
      </div>
      <div class="row">
        <div class="col-sm-6">
          <div class="form-group">
            <label class="col-lg-4 control-label">Category</label>
              <div class="col-lg-8">
                <select name="category" class="form-control chosen">
                  <option value="">--</option>
                  <?php foreach($category as $c): ?>
                    <option value="<?php echo $c->category_id; ?>" <?php echo ($ic->category == $c->category_id ? 'selected' : ''); ?> ><?php echo $c->category_name; ?></option>
                  <?php endforeach; ?>
                </select>
              </div>
          </div>
        </div>
        <div class="col-sm-6"> 
          <div class="form-group">
            <label class="col-lg-4 control-label">Sub Category</label>
              <div class="col-lg-8">
              <input type="hidden" name="sub_cat_temp" value="<?php echo $ic->sub_category; ?>">
                <select name="sub_category" class="form-control chosen">
                  <option value="">--</option>
                  <?php foreach($sub_category as $sc): ?>
                  <option value="<?php echo $sc->lookup_code; ?>" <?php echo ($ic->sub_category == $sc->lookup_code ? 'selected' : ''); ?>><?php echo $sc->meaning; ?></option>
                  <?php endforeach; ?>
                </select>
              </div>
          </div>
        </div>
      </div>
      <div class="row">
        <div class="col-sm-6">
           <div class="form-group">
            <label class="col-lg-4 control-label">Case Title</label>
              <div class="col-lg-8">
                <input type="text" name="case_title" value="<?php echo $ic->case_title; ?>" class="form-control">
              </div>
          </div>
        </div>
        <div class="col-sm-6">
          <div class="form-group">
            <label class="col-lg-4 control-label">Desired Date</label>
              <div class="col-lg-8">
                <input type="date" value="<?php echo $ic->desired_date; ?>" name="desired_date" class="form-control">
              </div>
          </div>
        </div>
      </div>
      <div class="row">
        <div class="col-sm-12">
          <div class="form-group">
            <label class="col-lg-2 control-label">Case Details</label>
              <div class="col-lg-10">
                <textarea name="case_details" class="form-control">
                  <?php echo $ic->case_details; ?>
                </textarea>
              </div>
          </div>
        </div>
      </div>
      <div class="row">
        <div class="col-sm-6">
          <div class="form-group">
            <label class="col-lg-4 control-label">Department</label>
              <div class="col-lg-8">
                <select name="department" class="form-control chosen">
                  <option value="">--</option>
                  <?php foreach($department as $dep): ?>
                  <option value="<?php echo $dep->lookup_code; ?>" <?php echo ($ic->department == $dep->lookup_code ? 'selected' : ''); ?>><?php echo $dep->meaning; ?></option>
                  <?php endforeach; ?>
                </select>
              </div>
          </div>
        </div>
        <div class="col-sm-6">
          <div class="form-group">
            <label class="col-lg-4 control-label">Assign To</label>
              <div class="col-lg-8">
                <select name="assigned_to" class="form-control chosen">
                  <option value="">--</option>
                  <?php foreach($assigned_to as $ass): ?>
                  <option value="<?php echo $ass->user_id; ?>" <?php echo ($ic->assigned_to == $ass->user_id ? 'selected' : ''); ?> ><?php echo $ass->first_name." ".$ass->last_name; ?></option>
                  <?php endforeach; ?>
                </select>
              </div>
          </div>
      </div>
      </div>
      <div class="row">
        <div class="col-sm-6">
           <div class="form-group">
            <label class="col-lg-4 control-label">Impact</label>
              <div class="col-lg-8">
                <select name="impact" class="form-control chosen">
                  <option value="">--</option>
                  <?php foreach($impact as $imp): ?>
                  <option value="<?php echo $imp->lookup_code; ?>" <?php echo ($ic->impact == $imp->lookup_code ? 'selected' : ''); ?> ><?php echo $imp->meaning; ?></option>
                  <?php endforeach; ?>
                </select>
              </div>
          </div>
        </div>
        <div class="col-sm-6">
          <div class="form-group">
            <label class="col-lg-4 control-label">Urgency</label>
              <div class="col-lg-8">
                <select name="urgency" class="form-control chosen">
                  <option value="">--</option>
                  <?php foreach($urgency as $ur): ?>
                  <option value="<?php echo $ur->lookup_code; ?>" <?php echo ($ic->urgency == $ur->lookup_code ? 'selected' : ''); ?>><?php echo $ur->meaning; ?></option>
                  <?php endforeach; ?>
                </select>
              </div>
            </div>
          </div>
      </div>
      <div class="row">
        <div class="col-sm-6">
          <div class="form-group">
            <label class="col-lg-4 control-label">Activity Status</label>
              <div class="col-lg-8">
                <select name="activity_status" class="form-control chosen">
                  <option value="">--</option>
                  <?php foreach($activity_status as $as): ?>
                  <option value="<?php echo $as->lookup_code; ?>" <?php echo ($ic->activity_status == $as->lookup_code ? 'selected' : ''); ?> ><?php echo $as->meaning; ?></option>
                  <?php endforeach; ?>
                </select>
              </div>
          </div>
        </div>
        <div class="col-sm-6">
          <div class="form-group">
            <label class="col-lg-4 control-label">Case Status</label>
              <div class="col-lg-8">
                <select name="case_status" class="form-control chosen">
                  <option value="">--</option>
                  <?php foreach($case_status as $cst): ?>
                  <option value="<?php echo $cst->lookup_code; ?>" <?php echo ($ic->case_status == $cst->lookup_code ? 'selected' : ''); ?>><?php echo $cst->meaning; ?></option>
                  <?php endforeach; ?>
                </select>
              </div>
          </div>
        </div>
      </div>
      <div class="row">
        <div class="col-sm-12">
          <div class="form-group">
            <label class="col-lg-2 control-label">Root Cause</label>
              <div class="col-lg-10">
                <textarea name="root_cause" class="form-control">
                  <?php echo $ic->root_cause; ?>
                </textarea>
              </div>
          </div>
        </div>
      </div>
      <div class="row">
         <div class="col-sm-6">
          <div class="form-group">
            <label class="col-lg-4 control-label">Date Resolved</label>
              <div class="col-lg-8">
                <input type="date" value="<?php echo $ic->date_resolved; ?>" name="date_resolved" class="form-control">
              </div>
          </div>
        </div>
      </div>
      </form>
      <?php endforeach; ?>
      <!-- <button data-toggle="button" class="btn btn-primary btn-sm btn-outline" id="show_hist_btn_edit">Show History</button> -->
    </div>
    <div class="ibox-content" id="cases_history_edit" style="display: ;">
        <div class="chat-discussion" id="im_case_hist2">
        </div>
    </div>
    <div class="ibox-content" id="">
      <table class="table table-hovered">
        <thead>
            <tr>
                <th>Reference Doc Type</th>
                <th>Reference Doc No.</th>
            </tr>
        </thead>
        <tbody id="">
          <?php foreach ($work_order_case as $woc): ?>
          <tr>
            <td>Work Order</td>
            <td><?php echo $woc->work_order_no; ?></td>
          </tr>
        <?php endforeach; ?>
        <?php foreach ($invoice_case as $icc): ?>
          <tr>
            <td>Invoice</td>
            <td><?php echo $icc->invoice_num; ?></td>
          </tr>
        <?php endforeach; ?>
        </tbody>
      </table>
    </div>
 </div>
 <div class="modal inmodal in" id="hist_modal_add" tabindex="-1" role="dialog">
    <div class="modal-dialog">
    <div class="modal-content animated bounceInRight">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
            </div>
            <div class="modal-body">
              <form method="post" class="form-horizontal" id="">
                <div class="form-group" id="action_field">
                  <label class="col-lg-4 control-label">Action</label>
                    <div class="col-lg-5">
                      <select name="action" class="form-control chosen">
                        <option value="">--</option>
                        <?php foreach($action as $as): ?>
                        <option value="<?php echo $as->lookup_code; ?>"><?php echo $as->meaning; ?></option>
                        <?php endforeach; ?>
                      </select>
                    </div>
                </div>
                <div class="form-group assigned_to_hist">
                  <label class="col-lg-4 control-label">Department</label>
                    <div class="col-lg-5">
                      <select name="department" class="form-control chosen">
                        <option value="">--</option>
                        <?php foreach($department as $dep): ?>
                        <option value="<?php echo $dep->lookup_code; ?>" ><?php echo $dep->meaning; ?></option>
                        <?php endforeach; ?>
                      </select>
                    </div>
                </div>
                <div class="form-group assigned_to_hist">
                  <label class="col-lg-4 control-label">Assign To</label>
                    <div class="col-lg-5">
                      <select disabled name="assigned_to" class="form-control chosen">
                        <option value="">--</option>
                      </select>
                    </div>
                </div>
                <div class="form-group">
                  <label class="col-lg-4 control-label">Comments</label>
                    <div class="col-lg-5">
                      <textarea name="comments" class="form-control">
                      </textarea>
                    </div>
                </div>
                <div class="form-group" style="display: none;">
                  <label class="col-lg-4 control-label">Root Cause</label>
                    <div class="col-lg-5">
                      <textarea name="root_cause" class="form-control">
                      </textarea>
                    </div>
                </div>
                <div class="form-group">
                  <label class="col-lg-4 control-label">Action Date</label>
                    <div class="col-lg-5">
                      <input type="date" value="<?php echo date('Y-m-d'); ?>" name="action_date" class="form-control">
                    </div>
                </div>
              </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-white" data-dismiss="modal">Close</button>
                <button type="button" id="" class="btn btn-primary">Save</button>
            </div>
        </div>
    </div>
</div>
 <script>
    $(document).ready(function(){
            jQuery(".timeago").timeago();
            $(".assigned_to_hist").hide();
            get_history_by_case_id_v();
            $('#complete_btn_v').show();
            $('textarea').html('');
            <?php foreach($im_cases as $ic): ?>
            $('#im_request_form_view textarea[name=root_cause]').html("<?php echo filter_var($ic->root_cause,FILTER_SANITIZE_STRING); ?>");
            $('#im_request_form_view textarea[name=case_details]').html("<?php echo filter_var($ic->case_details,FILTER_SANITIZE_STRING); ?>");
            $('#im_request_form_view textarea[name=caller_address]').html("<?php echo filter_var($ic->caller_address,FILTER_SANITIZE_STRING); ?>");
            <?php endforeach;?>
            <?php if(!$userrole || $case_stats): ?>
              $("#im_request_form_view").find(':input:not(:disabled)').prop('disabled',true);
              $("#im_request_form_view").find('select').prop('disabled',true);
            <?php endif; ?>
            
            $('select').trigger('chosen:updated');
            $('.input-group.date').datepicker({
                todayBtn: "linked",
                clearBtn: true,
                autoclose: true,
                todayHighlight: true
            });

            $('.i-checks').iCheck({
                    checkboxClass: 'icheckbox_square-green',
                    radioClass: 'iradio_square-green',
            });

            $('select').chosen({width : "100%"});

            var user_list_e = new Array();

            <?php foreach($assigned_to as $ass): ?>
              user_list_e.push({user_id : "<?php echo $ass->user_id; ?>", name: '<?php echo $ass->first_name." ".$ass->last_name; ?>', department : "<?php echo $ass->department; ?>",user_name: '<?php echo $ass->user_name ?>'});
            <?php endforeach; ?>

            getSubCat($('#im_request_form_view select[name=category]').val());
            getSubCatByCatId($('#im_request_form_view select[name=category]').val());
            $(document).on('change','#im_request_form_view select[name=category]',function(){
              getSubCat($(this).val());
              getSubCatByCatId($(this).val());
              $('select').trigger('chosen:updated');
            });

            function getSubCatByCatId(id){
              var sub_cat = $('#im_request_form_view input[name=sub_cat_temp]').val();
              $.ajax({
                type: "POST",
                url: "<?php echo base_url($this->session->userdata('forajax').'/get_sub_category_by_category_id'); ?>",
                data: { "id" : id },
                dataType: 'json',
                async: true,
                success: function(data){
                    var tmp = "";
                    if(data.length > 0){
                      tmp += "<option value=''>--</option>";
                      $.each(data, function(i, index) {
                        tmp += "<option "+ (sub_cat == data[i].sub_category_id ? 'selected' : '') +" value='"+ data[i].sub_category_id +"'>" + data[i].sub_category_name + "</option>";

                      });
                    }

                     $('#im_request_form_view select[name=sub_category]').html(tmp);
                     $('select').trigger('chosen:updated');
                }
              });
            }
            function getSubCat(id){
               $.ajax({
                type: "POST",
                url: "<?php echo base_url($this->session->userdata('forajax').'/get_category_by_id'); ?>",
                data: { "id" : id },
                dataType: 'json',
                async: true,
                success: function(data){
                    var tmp = "";
                    if(data.length > 0){
                      $.each(data, function(i, item) {
                        if(data[i].default_department != null || data[i].default_department != ''){
                          $('#im_request_form_view select[name=department]').val(data[i].default_department);
                          getAss(data[i].default_department, data[i].default_assign_to);
                        }
                      });
                    $('select').trigger('chosen:updated');
                    }
                }
              });
            }

            $('#im_request_form_view select[name=sub_category]').val();
            $('select').trigger('chosen:updated');

            $(document).on('change','#im_request_form_view select[name=sub_category]',function(){
              $.ajax({
                type: "POST",
                url: "<?php echo base_url($this->session->userdata('forajax').'/get_sub_category_by_id'); ?>",
                data: { "id" : $(this).val() },
                dataType: 'json',
                async: true,
                success: function(data){
                    var tmp = "";
                    if(data.length > 0){
                      $.each(data, function(i, item) {
                        if(data[i].default_department != null || data[i].default_department != ''){
                          $('#im_request_form_view select[name=department]').val(data[i].default_department);
                          getAss(data[i].default_department,data[i].default_assign_to);
                          $('select').trigger('chosen:updated');
                        }
                        if(data[i].default_assign_to != null || data[i].default_assign_to != ''){
                          $('#im_request_form_view select[name=assigned_to]').val(data[i].default_assign_to);
                          $('#im_request_form_view select[name=activity_status]').val("ASSIGNED");
                          $('select').trigger('chosen:updated');
                        }
                      });
                    }
                }
              });
            });

            $(document).on('change','#im_request_form_view select[name=assigned_to]',function(){
              //alert($(this).val());
              if($(this).val() !== ''){
                $('#im_request_form_view select[name=activity_status]').val("ASSIGNED");
              }else{
                $('#im_request_form_view select[name=activity_status]').val("UNASSIGNED");
              }
              $('select').trigger('chosen:updated');
            });

            getAss( $('#im_request_form_view select[name=department]').val(), $('#im_request_form_view select[name=assigned_to]').val());
            $(document).on('change','#im_request_form_view select[name=department]',function(){
              getAss($(this).val(),null);
            });

            function getAss(id,uid){
              var tmp = "";
              console.log(uid);
              tmp += "<option value=''>--</option>";
              $.each(user_list_e, function(i, item) {
                if(user_list_e[i].department == id){
                   tmp += "<option value='"+ user_list_e[i].user_id+"' "+ (uid == user_list_e[i].user_id ? 'selected' : '') +">"+user_list_e[i].name + " (" + user_list_e[i].user_name+ ")" +"</option>";
                }
              });
              $('#im_request_form_view select[name=assigned_to]').html(tmp);
              if(uid !== null){
                $('#im_request_form_view select[name=activity_status]').val("ASSIGNED");
              }else{
                $('#im_request_form_view select[name=activity_status]').val("UNASSIGNED");
              }
              $('select').trigger('chosen:updated');
            }

            $(document).on('change','#hist_modal_add select[name=action]',function(){
              switch ($(this).val()) {
                case 'CLOSED':
                  $('#hist_form_add #root_cause_field').show();
                  $('#hist_form_add textarea[name=root_cause]').removeAttr('disabled');

                  $('#hist_form_add .assigned_to_hist').hide();
                  $('#hist_form_add select[name=assigned_to]').attr('disabled',true);
                  break;
                case 'ASSIGNED':
                  $('#hist_form_add .assigned_to_hist').show();
                  $('#hist_form_add select[name=assigned_to]').removeAttr('disabled');

                  $('#hist_form_add #root_cause_field').hide();
                  $('#hist_form_add textarea[name=root_cause]').attr('disabled',true);
                  break;
                default:
                  $('#hist_form_add #root_cause_field').hide();
                  $('#hist_form_add textarea[name=root_cause]').attr('disabled',true);
                  $('#hist_form_add .assigned_to_hist').hide();
                  $('#hist_form_add select[name=assigned_to]').attr('disabled',true);
                  break;
              }
              $('select').trigger('chosen:updated');
            });

            $(document).on('change','#hist_form_add select[name=department]',function(){
              getUserByDepartment($(this).val());
            });

            function getUserByDepartment(id){
              $.ajax({
                type: "POST",
                url: "<?php echo base_url($this->session->userdata('forajax').'/get_user_by_department'); ?>",
                data: { "id" : id },
                dataType: 'json',
                async: true,
                success: function(data){
                    var tmp = "";
                    tmp += "<option>--</option>";
                    if(data.length > 0){
                      $.each(data, function(i, item) {
                       tmp += "<option value='"+ data[i].user_id+"'>"+data[i].first_name + " " + data[i].last_name + " (" + data[i].user_name+ ")" +"</option>";
                      });
                    }
                    $('#hist_form_add select[name=assigned_to]').html(tmp);
                    $('select').trigger('chosen:updated');
                }
              });
            }

            $(document).on('change','#im_request_form_view select[name=activity_status]',function(){
              switch ($(this).val()) {
                case 'UNASSIGNED':
                  $('#im_request_form_view select[name=assigned_to]').attr('disabled',true);
                  break;
                default:
                  $('#im_request_form_view select[name=assigned_to]').removeAttr('disabled');
                  break;
              }
              $('select').trigger('chosen:updated');
            });

            $(document).on('change','#hist_form_add select[name=related_doc_type]',function(){
              get_related_doc_type_values($(this).val());
            });

            $(document).on('change','#im_request_form_view select[name=case_type]',function(){
              switch ($(this).val()) {
                case 'CMP':
                  $('#im_request_form_view input[name=caller_id]').removeAttr('disabled');
                  $('#im_request_form_view input[name=caller_acct_id]').removeAttr('disabled');
                  $('#im_request_form_view input[name=caller_name]').removeAttr('disabled');
                  $('#im_request_form_view textarea[name=caller_address]').removeAttr('disabled');
                  $('#im_request_form_view input[name=caller_contact]').removeAttr('disabled');
                  break;
                  case 'REQ':
                  $('#im_request_form_view input[name=caller_id]').removeAttr('disabled');
                  $('#im_request_form_view input[name=caller_acct_id]').removeAttr('disabled');
                  $('#im_request_form_view input[name=caller_name]').removeAttr('disabled');
                  $('#im_request_form_view textarea[name=caller_address]').removeAttr('disabled');
                  $('#im_request_form_view input[name=caller_contact]').removeAttr('disabled');
                  break;
                default:
                  $('#im_request_form_view input[name=caller_id]').attr('disabled',true);
                  $('#im_request_form_view input[name=caller_acct_id]').attr('disabled',true);
                  $('#im_request_form_view input[name=caller_name]').attr('disabled',true);
                  $('#im_request_form_view textarea[name=caller_address]').attr('disabled',true);
                  $('#im_request_form_view input[name=caller_contact]').attr('disabled',true);
                  break;
              }
              $('select').trigger('chosen:updated');
            });

            function get_history_by_case_id_v(){
              $.ajax({
                type: "POST",
                url: "<?php echo base_url($this->session->userdata('forajax').'/get_history_by_case_id'); ?>",
                data: {"id": $('#im_request_form_view input[name=case_id]').val()},
                dataType: 'json',
                success: function(data){
                  var tmp =''; var tmp2 ='';
                  $.each(data, function(i, item) {
                    tmp2 += '<div class="chat-element">'
                        +  '<a href="#" class="pull-left">'
                        +  '<img alt="image" class="img-circle" src="<?php echo site_url('assets/img/logo.png'); ?>">'
                        +  '</a>'
                        +  '<div class="media-body ">'
                        +  '<small class="pull-right text-navy"><span class="label label-primary">'+ data[i].action_m +'</span>'+ jQuery.timeago(data[i].action_date) +'</small>'
                        +  '<strong>'+ data[i].action_by_m +'</strong><br/>'
                        +  '<p class="m-b-xs">'
                        +  data[i].comments
                        +  '</p>'
                        +  '<small class="text-muted">'+ data[i].action_date +'</small>'
                        +  '</div>'
                        +  '</div>';
                    tmp += '<div class="chat-message left">'
                        +  '<img class="message-avatar" src="<?php echo site_url("/assets/img/user_icon.png"); ?>" alt="">'
                        +  '<div class="message">'
                        +  '<a class="message-author" href="#"> '+ data[i].action_by_m +'</a>'
                        +  '<span class="message-date"><span class="label label-default">'+ data[i].action_m +'</span>&nbsp;&nbsp;&nbsp;&nbsp;<small class="text-muted">'+ data[i].action_date +' </small><span class="text-navy">'+ jQuery.timeago(data[i].action_date) +'</span></span>'
                        +  '<span class="message-content">'
                        +  data[i].comments
                        +  '</span>'
                        +  '</div>'
                        +  '</div>';
                  });
                  $('#im_case_hist2').html(tmp);
                }
              });
            }

            function get_related_doc_type_values(type){
              $.ajax({
                type: "POST",
                url: "<?php echo base_url($this->session->userdata('forajax').'/get_related_doc_type_values'); ?>",
                data: {"type": type },
                dataType: 'json',
                success: function(data){
                  var tmp = '';
                  $.each(data, function(i, item) {
                    tmp += '<option value="'+ data[i].id +'">' + data[i].num + '</option>'; 
                  });
                  $('#hist_form_add select[name=related_doc_no]').html(tmp);
                  $('select').trigger('chosen:updated');
                }
              });
            }

            $('#im_request_form_view').validate({
                 rules: {
                     case_no: {
                        required: true,
                        number: true
                     },
                     case_date: {
                      required : true
                     },
                     case_type: {
                      required : true
                     },
                     category: {
                      required : true
                     },
                     sub_category: {
                      required : true
                     },
                     impact: {
                      required : true
                     },
                     urgency: {
                      required : true
                     },
                     case_title: {
                      required : true
                     },
                     case_details: {
                      required : true
                     },
                     desired_date: {
                      required : true
                     },
                     department: {
                      required : true
                     },
                     assigned_to: {
                      required : true
                     },
                     activity_status: {
                      required : true
                     },
                     case_status: {
                      required : true
                     },
                     caller_acct_id: {
                      required : true
                     },
                     caller_name: {
                      required : true
                     },
                     caller_address: {
                      required : true
                     },
                     caller_contact: {
                      required : true
                     }
                 }
            });

            $('#hist_form_add').validate({
                 rules: {
                     action: {
                        required: true
                     },
                     comments: {
                      required : true
                     },
                     action_date: {
                      required : true
                     }
                 }

                     /*related_doc_type: {
                      required : true
                     },
                     related_doc_id: {
                      required : true
                     },*/
            });

            function alert_cancel(){
              if($('#uom_cn').val() != "" || $('#uom_c').val() != "" || $('#uom_n').val() != "" || $('#uom_cb').is(':checked') == false){
                  swal({
                        title: "Are you sure?",
                        text: "Your work will not be save",
                        type: "warning",
                        showCancelButton: true,
                        confirmButtonColor: "#DD6B55",
                        confirmButtonText: "Leave",
                        cancelButtonText: "Stay",
                        closeOnConfirm: true,
                        closeOnCancel: true },
                    function (isConfirm) {
                        if (isConfirm) {
                          swal("success");
                            close_view(); active_search();
                            notifymsg("hide","hide");
                        } else {
                            swal("Cancelled");
                        }
                    });
              }else{
                  close_view(); active_search();
                  notifymsg("hide","hide");
              }
            }
            $('#cancel_view').click(function(){
                alert_cancel();
            });
            $('#complete_btn_v').click(function(){
                close_view(); active_search();
            });

            $('#viewX').click(function(){
              alert_cancel();      
            });


    });
 </script>  